package com.branches.chapeter_4;

import com.branches.SleepUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;

public class NWConcurrency {
    static boolean flag = true;

    static Object lock = new Object();

    public static void main(String[] args) throws Exception {
        Thread waitThread = new Thread(new Wait(), "WaitThread");
        waitThread.start();

        TimeUnit.SECONDS.sleep(1);
        Thread notifyThread = new Thread(new Notify(), "NotifyThread");
        notifyThread.start();

    }

    public static String getDate() {
        return new SimpleDateFormat(" HH: mm: ss").format(new Date());
    }

    static class Wait implements Runnable {
        @Override
        public void run() {
            synchronized(lock) {
                while (flag) {
                    System.out.println(Thread.currentThread() + " flag is true. wa @ " + getDate() );
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println(Thread.currentThread() + " flag is false. running @ " + getDate());
            }
        }
    }

    static class Notify implements Runnable {

        @Override
        public void run() {
            synchronized(lock) {
                System.out.println(Thread.currentThread() + " hold lock. notify @ " + getDate());
                lock.notifyAll();
                flag = false;
                SleepUtils.second(6);
            }

            synchronized (lock) {
                System.out.println(Thread.currentThread() + "hold lock again. sleep @ " + getDate());
                SleepUtils.second(6);
            }
        }
    }
}
